//
//  ViewController.m
//  跟人中心导航栏变化动画
//
//  Created by unisedu on 16/3/7.
//  Copyright © 2016年 CaoFei. All rights reserved.
//

#import "ViewController.h"
#define OBSERVERKEY @"contentOffset"            ///<    监听属性
#define MAXMOVE 125.0                                        ///<    滚动最大值
#define TITLEMAXMOVE 16.0                              ///<    导航文本位移最大值
#define IMAGETOPANDBOTTOMCONSTANT 30.0
#define TOPANDBOTTOMCONSTANT 0.0             ///<    红色背景上下间距约束值
#define LEFTANDRIGHTCONSTANT -150.0
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) UITableView *tableView;

@property (weak, nonatomic) IBOutlet UIView *backgroundView;
@property (weak, nonatomic) IBOutlet UIView *alphaContentView;
@property (weak, nonatomic) IBOutlet UILabel *navigationTitle;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *backgroundTopConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *labelBottomConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentTopConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *infoCenterX;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *infoCenterY;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UITableView * tableView = [UITableView new];
    [tableView setDataSource:self];
    [tableView  setDelegate:self];
    [self setTableView:tableView];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear: animated];
    [self.tableView addObserver: self forKeyPath: OBSERVERKEY options: NSKeyValueObservingOptionNew context: nil];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [self.tableView removeObserver: self forKeyPath: OBSERVERKEY];
}

#pragma mark - <UITableViewDataSource>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: @"cell"];
    [cell.textLabel setText:@"1111"];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 100;
}
#pragma mark - Observe
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    CGFloat yOffset = self.tableView.contentOffset.y / MAXMOVE;
    yOffset = MAX(0, MIN(1, yOffset)); //小于1，大于0
    self.navigationTitle.alpha = 1 - yOffset;
    self.labelBottomConstraint.constant = yOffset * TITLEMAXMOVE;
    self.backgroundTopConstraint.constant = self.contentTopConstraint.constant = TOPANDBOTTOMCONSTANT - yOffset * MAXMOVE;
    self.infoCenterX.constant = IMAGETOPANDBOTTOMCONSTANT * yOffset;
    self.infoCenterY.constant = LEFTANDRIGHTCONSTANT * yOffset;
}


@end
